package com.umanji.message;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.umanji.category.domain.Categories;
import com.umanji.category.service.CategoryService;
import com.umanji.usedmarket.domain.UsedProducts;
import com.umanji.usedmarket.service.UsedProductService;
import com.umanji.user.UserController;

/**
 * Handles requests for the application welcome page.
 */
@Controller
@RequestMapping(value="/message")
public class MessageController {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(UserController.class);

	private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
	
	@Autowired
	public MessageController(Validator validator) {
		this.validator = validator;
	}

    @Autowired
    private UsedProductService usedProductService;

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelMap newCategoryForm(ModelMap model) {
		logger.info("register category form!");

		UsedProducts usedProduct = new UsedProducts( );
        model.addObject("umj_used_product", usedProduct);
        return model;

	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@ModelAttribute("umj_form_category") Categories umanji_categories, BindingResult result) {
		logger.info("register user!");

        for (ConstraintViolation<Categories> constraint : validator.validate(umanji_categories)) {
            result.rejectValue(constraint.getPropertyPath().toString(), "", constraint.getMessage());
        }
        if (result.hasErrors())
        	return "/umj/category/create";

        return "redirect:/umj/category/create";
	}
}
